/*
 *  Property Mapper, simple automatic GUI generator for JavaBeans using annotations.
 *  Copyright (C) 2008 Jon Lennersten, Johan Hedin
 *
 *  This file is part of Property Mapper.
 *
 *  Property Mapper is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Property Mapper is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Property Mapper.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.mawell.common.propertymapper;

import java.util.HashMap;

/**
 * A simple that counting visitor for property configurations.
 * Counts the number of times different configuration types have been visited.
 * 
 * Property configuration containers are also recursively visited.
 * 
 * @author Tatu Tarvainen (tatu.tarvainen@mawell.com)
 */
public class CountingPropertyConfigurationVisitor implements PropertyConfigurationVisitor {

    private HashMap<Class<? extends PropertyConfiguration>, Integer> counts =
        new HashMap<Class<? extends PropertyConfiguration>, Integer>();
        
    private void inc(Class<? extends PropertyConfiguration> cls) {
        if(counts.containsKey(cls))
            counts.put(cls, counts.get(cls)+1);
        else
            counts.put(cls, 1);
    }
    
    public void visit(BooleanPropertyConfiguration bpc) {
        inc(BooleanPropertyConfiguration.class);
    }

    public void visit(DatePropertyConfiguration dpc) {
        inc(DatePropertyConfiguration.class);
    }

    public void visit(EnumPropertyConfiguration epc) {
        inc(EnumPropertyConfiguration.class);
    }

    public void visit(IntPropertyConfiguration ipc) {
        inc(IntPropertyConfiguration.class);
    }

    public void visit(MapPropertyConfiguration mpc) {
        inc(MapPropertyConfiguration.class);
    }

    public void visit(ListPropertyConfiguration lpc) {
        inc(ListPropertyConfiguration.class);
    }

    public void visit(PropertyConfigurationContainer pcc) {
        inc(PropertyConfigurationContainer.class);
        for(PropertyConfiguration pc : pcc.getPropertyConfigurations())
            pc.accept(this);
    }

    public void visit(StringPropertyConfiguration spc) {
        inc(StringPropertyConfiguration.class);
    }

    public int getCountForType(Class<? extends PropertyConfiguration> cls) {
        if(counts.containsKey(cls))
            return counts.get(cls);
        else 
            return 0;
    }
}
